/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.jude.rollviewpagerdome.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.window.dialog.ToastDialog;

import com.jude.rollviewpager.Until;
import com.jude.rollviewpager.adapter.PageSliderAdapter;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpagerdome.ResourceTable;

import java.util.ArrayList;

/**
 * MainAbility
 *
 * @since 2021-03-23
 */
public class NetImageAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    RollPagerView rollPagerView;
    Button mbutton;
    Button abutton;
    private int number = 0;
    private final int mNum3 = 3;
    private final int mNum4 = 4;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_netimage);
        rollPagerView = (RollPagerView) findComponentById(ResourceTable.Id_viewpageslider3);
        ArrayList<Integer> pages = new ArrayList<>();
        pages.add(ResourceTable.Media_loading);
        pages.add(ResourceTable.Media_loading);
        pages.add(ResourceTable.Media_loading);
        pages.add(ResourceTable.Media_loading);
        pages.add(ResourceTable.Media_loading);
        PageSliderAdapter bannerAdapter2 = new PageSliderAdapter(pages, getContext());
        rollPagerView.setadapter(bannerAdapter2, pages);
        rollPagerView.setClickedListener(this::onClick);
        mbutton = (Button) findComponentById(ResourceTable.Id_Sbutton);
        mbutton.setClickedListener(this::onClick);
        abutton = (Button) findComponentById(ResourceTable.Id_Xbutton);
        abutton.setClickedListener(this::onClick);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_viewpageslider3:
                int post = rollPagerView.getpost();
                new ToastDialog(getContext()).setText("Item" + post + "clicked").show(); // Item 2 clicked
                break;
            case ResourceTable.Id_Sbutton:
                int num = rollPagerView.getpost();
                if (num == 0) {
                    rollPagerView.setNext(num);
                } else {
                    rollPagerView.setNext(num - 1);
                }
                break;
            case ResourceTable.Id_Xbutton:
                Until.setUge(false);
                number++;
                if (number == mNum3) {
                    rollPagerView.setList();
                }
                int index = rollPagerView.getpost();
                if (index == mNum4) {
                    rollPagerView.setNext(0);
                } else {
                    rollPagerView.setNext(index + 1);
                }
                break;
            default:
                break;
        }
    }
}
